package com.ebt.m.proposal_v2.ui.view;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;

import com.ebt.m.R;
import com.ebt.m.data.entity.Customer;
import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.bean.InsuredEntity;
import com.ebt.m.proposal_v2.dao.response.BusinessConfig;
import com.ebt.m.proposal_v2.event.AddAttachProductEvent;
import com.ebt.m.proposal_v2.event.InsuredChangedEvent;
import com.ebt.m.proposal_v2.event.ProductChangedEvent;
import com.ebt.m.proposal_v2.event.ProductsChangedEvent;
import com.ebt.m.proposal_v2.utils.DataUtils;
import com.ebt.m.proposal_v2.widget.base.CompatLinearLayout;
import com.ebt.m.proposal_v2.widget.view.InsuranceCellView;
import com.ebt.m.commons.utils.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 被保人UI
 *
 * @author archer.qi
 *         Created on 2017/5/9.
 */
public class InsuredAndInsuranceView extends CompatLinearLayout {

    @BindView(R.id.insured)
    InsuredView mInsuredView;
    @BindView(R.id.insurance)
    InsuranceView mInsuranceView;

    private static HashMap<Integer, String> insuredTitles = new HashMap<>();

    static {
        insuredTitles.put(0, "第二被保人信息");
        insuredTitles.put(1, "第三被保人信息");
        insuredTitles.put(2, "第四被保人信息");
        insuredTitles.put(3, "第五被保人信息");
        insuredTitles.put(4, "第六被保人信息");
        insuredTitles.put(5, "第七被保人信息");
        insuredTitles.put(6, "第八被保人信息");
        insuredTitles.put(7, "第九被保人信息");
        insuredTitles.put(8, "第十被保人信息");
    }

    private static HashMap<Integer, String> insuranceTitles = new HashMap<>();

    static {
        insuranceTitles.put(0, "第二被保人险种信息");
        insuranceTitles.put(1, "第三被保人险种信息");
        insuranceTitles.put(2, "第四被保人险种信息");
        insuranceTitles.put(3, "第五被保人险种信息");
        insuranceTitles.put(4, "第六被保人险种信息");
        insuranceTitles.put(5, "第七被保人险种信息");
        insuranceTitles.put(6, "第八被保人险种信息");
        insuranceTitles.put(7, "第九被保人险种信息");
        insuranceTitles.put(8, "第十被保人险种信息");
    }

    public InsuredAndInsuranceView(Context context) {
        super(context);
    }

    public InsuredAndInsuranceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int setContentView() {
        return R.layout.proposal_v2_insured_and_insurance;
    }

    @Override
    protected void onBindView() {
        mInsuredView.setForMoreInsuredView(true);
        mInsuranceView.setForMoreInsuredView(true);
        initInsuredView();
        initInsuranceViews();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mInsuredView.setCorrespondingInsuranceView(mInsuranceView);
        mInsuredView.setCorrespondingApplicantView(mApplicantView);
        mInsuranceView.setCorrespondingInsuredView(mInsuredView);
        mInsuranceView.setCorrespondingApplicantView(mApplicantView);
    }

    public void updateTitleByIndex(int index) {
        String insuredTitle = getInsuredTitleByIndex(index);
        mInsuredView.setForMoreInsuredViewTitle(insuredTitle);
        String insuranceTitle = getInsuranceTitleByIndex(index);
        mInsuranceView.setForMoreInsuredViewTitle(insuranceTitle);
    }

    public List<InsuranceEntity> getInsuranceEntities() {
        return mInsuranceView.getProductsData();
    }

    public void initInsuredView() {
        mInsuredView.supportFragmentManager(((AppCompatActivity) getContext()).getSupportFragmentManager());
        mInsuredView.setInsuredChangedListener(new InsuredView.onInsuredChangedListener() {
            @Override
            public void onInsuredChanged(int type) {
                EventBus.getDefault().post(new InsuredChangedEvent(mInsuredView, mInsuranceView, type));
            }
        });
    }

    public void initInsuranceViews() {
        mInsuranceView.setFragmentManager(((AppCompatActivity) getContext()).getSupportFragmentManager());
        mInsuranceView.setInsuredPerson(mInsuredView.getInsured());
        mInsuranceView.setOnProductAddListener(new InsuranceView.OnProductAddListener() {
            @Override
            public void addMainProduct() {
            }

            @Override
            public void addAttachProduct(ArrayList<String> exists) {
                EventBus.getDefault().post(new AddAttachProductEvent(mInsuranceView, exists));
            }

            @Override
            public void deleteMainProduct() {
            }
        });

        mInsuranceView.setOnProductChangedListener(new InsuranceView.OnProductChangedListener() {
            @Override
            public void onProductChanged(InsuranceEntity data, int isMain, int action) {
                EventBus.getDefault().post(new ProductChangedEvent(mInsuranceView, data, isMain, action, true));
            }

            @Override
            public void onProductsChanged(List<InsuranceEntity> data) {
                EventBus.getDefault().post(new ProductsChangedEvent(mInsuranceView, data, true));
            }
        });
    }

    public ArrayList<String> getMoreInsuredAdditionalIds() {
        try {
            return mInsuranceView.getExistsAdditionalIds();
        } catch (Exception e) {
            Logger.e(e);
        }
        return null;
    }

    private String getInsuredTitleByIndex(int index) {
        if (insuredTitles.containsKey(index)) {
            return insuredTitles.get(index);
        }
        return "第" + index + "被保人信息";
    }

    private String getInsuranceTitleByIndex(int index) {
        if (insuranceTitles.containsKey(index)) {
            return insuranceTitles.get(index);
        }
        return "第" + index + "被保人险种信息";
    }

    public void updateDefaultBusinessConfig(BusinessConfig config) {
        mInsuredView.setBusinessConfig(config);
    }

    public int getRelation() {
        if (mInsuredView == null) {
            return -1;
        }
        return mInsuredView.getRelation();
    }

    public InsuredEntity getInsuredEntity() {
        List<InsuranceEntity> insuranceEntities = getInsuranceEntities();
        if (insuranceEntities == null || insuranceEntities.size() == 0) {
            return null;
        }
        Customer customer = mInsuredView.getCustomer();
        InsuredEntity insuredEntity = DataUtils.toInsured(customer);
        insuredEntity.risk = insuranceEntities;
        return insuredEntity;
    }

    public void updateContentByDraftEntity(InsuredEntity item) {
        if (mInsuredView != null) {
            mInsuredView.bindData(item);
        }
        if (mInsuranceView != null) {
            mInsuranceView.addRiskViews(item.risk);
        }
    }

    private ApplicantView mApplicantView;

    public void setCorrespondingApplicantView(ApplicantView applicantView) {
        this.mApplicantView = applicantView;
    }

    public void setOtherRelationCallBack(InsuredView.CheckOtherRelationCallBack callBack) {
        if (mInsuredView != null) {
            mInsuredView.setCheckOtherRelationCallBack(callBack);
        }
    }

    public InsuredView getInsuredView() {
        return mInsuredView;
    }

    /**
     * 检测产品投保选项是否匹配性别、年龄、职业类别
     *
     * @return
     */
    public ArrayList<Object> checkProductOption() {
        ArrayList<Object> retCellCheck = null;
        ArrayList<InsuranceCellView> cellViews = mInsuranceView.getInsuranceCellViews();
        for (InsuranceCellView cellView : cellViews) {
            retCellCheck = cellView.checkIfMatchCondition();
            if (!(Boolean) retCellCheck.get(0)) {
                return retCellCheck;
            }
        }
        return retCellCheck;
    }
}